<?php
declare(strict_types=1);

namespace app\controller\api\admin\general;

use think\App;
use think\Request;
use think\exception\ValidateException;

use app\controller\api\ApiBaseController;
use app\logic\api\admin\general\AttachmentLogic;
use app\validate\api\admin\general\AttachmentValidate;

class AttachmentController extends ApiBaseController
{
    protected $logic;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->logic = new AttachmentLogic();
    }

    public function index(Request $request)
    {
        $param = $request->param();

        $where = [];
        isset($param['file_ori_name']) && $param['file_ori_name'] != '' && $where[] = ['file_ori_name', 'like', '%' . $param['file_ori_name'] . '%'];
        isset($param['create_time']) && $param['create_time'] != '' && $where[] = ['create_time', 'between time', [$param['create_time'][0], $param['create_time'][1]]];

        $lgc_re = $this->logic->list($where, "*", ['id' => 'desc']);

        return $lgc_re['code'] == 200
            ? $this->success($lgc_re['msg'], $lgc_re['data'])
            : $this->error($lgc_re['msg']);
    }

    public function delete($id)
    {
        $data = $this->request->only([
            'del_ids'
        ]);

        try {
            validate(AttachmentValidate::class)->scene('delete')->check($data);

            $lgc_re = $this->logic->delete($data);

            return $lgc_re['code'] == 200
                ? $this->success($lgc_re['msg'])
                : $this->error($lgc_re['msg']);
        } catch (ValidateException $e) {
            return $this->error($e->getMessage());
        }
    }
}
